<template>
  <UploadExcel :on-success="success" />
</template>

<script>
import { importEmployees } from '@/api/employees'
export default {
  methods: {
    //   导入成功的函数
    async success(data) {
      console.log(data)
      const newData = data.results.map(item => {
        // console.log(item)
        return this.zh2en(item)
      })
      //   console.log(newData)
      await importEmployees(newData)
      this.$message.success('导入成功')
      this.$router.back()
    },
    zh2en(user) {
    //   console.log(user)
      const newUser = {}
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      //   第一种
      //   for (const key in user) {
      //     // console.log(key)// 中午key
      //     // console.log(user[key])// 值
      //     // console.log(dict[key])// 英文key
      //     newUser[dict[key]] = user[key]
      //     // console.log(dict)
      //   }
      // 第二种
      Object.keys(user).forEach(item => {
        // console.log(item)
        let value = user[item]
        if (dict[item] === 'timeOfEntry' || dict[item] === 'correctionTime') {
          value = new Date(this.formatDate(value, '/'))
        }
        newUser[dict[item]] = value
        // console.log(newUser)
      })
      return newUser
    },
    // 专门处理excel时间格式  format时间分割符
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style>

</style>
